package com.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dao.MovieListDAO;
import com.dao.ProgramCategoryDAO;
import com.pojo.MovieList;
import com.pojo.ProgramCategory;
import com.util.MD5Util;
import com.util.PortUtil;
import jodd.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;

import javax.print.attribute.standard.PageRanges;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Service
public class DyService extends ServiceImpl<MovieListDAO, MovieList> {


    private String server;

    @Value("${server.port}")
    private String port;

    @Autowired
    private MovieListDAO movieListDAO;

    @Autowired
    private ProgramCategoryDAO programCategoryDAO;


    /**
     * 获取某类型的电影
     *
     * @param page 第几页
     * @param size 页大小
     * @param type 电影类型
     * @return
     */
    public IPage page(int page, int size, Long type, String name) {
        server = System.getProperties().getProperty("server.ip");
        /*QueryWrapper<MovieList> q = new QueryWrapper();
        q.eq("is_it_on_the_shelf",1);
        q.eq("isdown",1);
        if (type!=null)
        {
            q.and(wrapper -> wrapper.eq("program_category_id", type).or().like("program_type", "%"+type+"%"));
        }
        if (name!=null && name.trim().length()>0)
        {
            q.like("program_name", "%"+name+"%");
        }
       */
        // q.orderByDesc("");

        Page p = new Page(page, size);
        IPage result = null;
        if (type != null && type == 0) {
            QueryWrapper q2 = new QueryWrapper();
            q2.eq("home_pag_recommendation", 1);
            q2.eq("is_it_on_the_shelf", 1);
            q2.eq("isdown", 1);
            //condition参数是否将该sql语句（像in()、like()）加在总sql语句上
            q2.orderBy(true, false, "recommended_time");
            result = movieListDAO.selectPage(new Page(page, size), q2);
        } else {
            Map m = new HashMap();
            if (name!=null && name.trim().length()>0)
            {
                m.put("name", name);
            }
            if (type!=null)
            {
                m.put("type", type);
            }
            if (type != null) {
                ProgramCategory programCategory = programCategoryDAO.selectById(type);
                String ids = programCategory.getMovie_ids();
                if (ids != null) {
                    String[] idsArry = ids.split(",");
                    if (idsArry!=null && idsArry.length>0)
                    {
                        m.put("list", idsArry);
                    }
                }
            }
            result = movieListDAO.getSortDyByType(p, m);  //page(p, q);
        }

        return result;
    }

    /**
     * 根据编号查找电影对象
     *
     * @param id
     * @return
     */
    public MovieList getById(long id) {
        MovieList obj = super.getById(id);
        if (obj != null) {
            String ip = null;
            String tempPort = port;
            if (PortUtil.checkPortIsOpen(8090)) {
                tempPort = "8090";
            }
            try {
                InputStream is = new FileInputStream("ip.properties");
                Properties p = new Properties();
                p.load(is);
                ip = p.getProperty("server.ip");
            } catch (Exception e) {
                //e.printStackTrace();
            }
            ip = ip + ":" + tempPort;

            if (ip.startsWith("http://") == false) {
                ip = "http://" + ip;
            }
            String playUrl = ip + "/dy/" + obj.getMd5();
            obj.setPlay_url(playUrl);
        }
        return obj;
    }


    /**
     * 根据拼音搜电影
     *
     * @param pinyin
     * @param page
     * @param size
     * @return
     */
    public IPage search(String pinyin, int page, int size) {
        Page p = new Page(page, size);
        QueryWrapper q = new QueryWrapper();
        q.like("program_name_yw", pinyin + "%");
        q.eq("is_it_on_the_shelf", 1);
        q.eq("isdown", 1);
        IPage result = movieListDAO.selectPage(p, q);
        return result;
    }

    public IPage searchName(String name, int page, int size) {
        Page p = new Page(page, size);
        QueryWrapper q = new QueryWrapper();
        if (StringUtil.isNotEmpty(name)) {
            q.like("program_name", "%" + name + "%");
        }
        q.eq("is_it_on_the_shelf", 1);
        q.eq("isdown", 1);
        IPage result = movieListDAO.selectPage(p, q);
        return result;
    }

    /**
     * 相关推荐
     *
     * @param dyid 电影类型和主演推荐
     * @param page
     * @param size
     * @return
     */
    public IPage tuijian(long dyid, int page, int size) {
        MovieList dy = movieListDAO.selectById(dyid);
        long type = dy.getProgram_category_id();  //电影类型
        String mains = dy.getPerformer();//主演
        String main = null;
        if (mains != null) {
            mains = mains.replaceFirst("主演", "");
            String[] arry = mains.split(":");
            if (arry == null || arry.length == 0) {
                arry = mains.split(",");
            }
            if (arry != null && arry.length > 0) {
                main = arry[0];
            }
        }
        Page p = new Page(page, size);
        QueryWrapper q = new QueryWrapper();
        q.like("program_type", "%" + type + "%");
        q.eq("is_it_on_the_shelf", 1);
        q.eq("isdown", 1);
        if (main != null) {
            q.like("performer", "%" + main + "%");
        }


        IPage result = movieListDAO.selectPage(p, q);
        if (result.getTotal() == 1) {
            QueryWrapper q2 = new QueryWrapper();
            q2.like("program_type", "%" + type + "%");
            q2.eq("isdown", 1);
            result = movieListDAO.selectPage(p, q2);
        }
        List<MovieList> list = result.getRecords();
        for (MovieList m : list) {
            if (m.getId() == dyid) {
                list.remove(m);
                break;
            }
        }
        return result;
    }

    /*
      删除下架电影
     */
    public void removeDownDy() {
        QueryWrapper q = new QueryWrapper();
        q.eq("is_it_on_the_shelf",0);
        movieListDAO.delete(q);
    }
}
